package com.zyd.shiro.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.entity.Enter;
import com.zyd.shiro.business.service.PakEnterService;
import com.zyd.shiro.framework.object.BaseConditionVO;
import com.zyd.shiro.persistence.beans.PakEnter;
import com.zyd.shiro.persistence.mapper.PakEnterMapper;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class PakEnterServiceImpl implements PakEnterService {

    @Autowired
    private PakEnterMapper pakEnterMapper;

    @Override
    public Enter insert(Enter enter) {
        Assert.notNull(enter, "对象不可为空！");
        enter.getPakEnter().setUpdateTime(new Date());
        enter.getPakEnter().setCreateTime(new Date());
        pakEnterMapper.insertSelective(enter.getPakEnter());
        return enter;
    }

    @Override
    public void insertList(List<Enter> enters) {
            for(Enter enter:enters){
               enter.getPakEnter().setUpdateTime(new Date());
               enter.getPakEnter().setCreateTime(new Date());
               pakEnterMapper.insertSelective(enter.getPakEnter());
           }
    }


    /**
     * 根据主键字段进行删除，方法参数必须包含完整的主键属性
     *
     * @param primaryKey
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByPrimaryKey(Long primaryKey) {
        return pakEnterMapper.deleteByPrimaryKey(primaryKey) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(Enter enter) {
        Assert.notNull(enter, "对象不可为空！");
        enter.setUpdateTime(new Date());
        return pakEnterMapper.updateByPrimaryKey(enter.getPakEnter()) > 0;
    }

    @Override
    public boolean updateSelective(Enter enter) {
        Assert.notNull(enter, "对象不可为空！");
        enter.setUpdateTime(new Date());
        return pakEnterMapper.updateByPrimaryKeySelective(enter.getPakEnter()) > 0;
    }

    /**
     * 根据主键字段进行查询，方法参数必须包含完整的主键属性，查询条件使用等号
     *
     * @param primaryKey
     * @return
     */

    @Override
    public Enter getByPrimaryKey(Long primaryKey) {
        PakEnter pakEnter = pakEnterMapper.selectByPrimaryKey(primaryKey);
        return null == pakEnter ? null : new Enter(pakEnter);
    }


    /**
     * 根据实体中的属性进行查询，只能有一个返回值，有多个结果时抛出异常，查询条件使用等号
     *
     * @param
     * @return
     */
    @Override
    public Enter getOneByEntity(Enter test) {
        Assert.notNull(test, "对象不可为空！");
        PakEnter pakEnter = pakEnterMapper.selectOne(test.getPakEnter());
        return null == pakEnter ? null : new Enter(pakEnter);
    }

    @Override
    public List<Enter> listAll() {
        List<PakEnter> pakEnters = pakEnterMapper.selectAll();
        if (CollectionUtils.isEmpty(pakEnters)) {
            return null;
        }
        List<Enter> enters = new ArrayList<>();
        for (PakEnter pakEnter : pakEnters) {
            enters.add(new Enter(pakEnter));
        }
        return enters;
    }

    @Override
    public List<Enter> listByEntity(Enter entity) {
        return null;
    }


    /**
     * 分页查询
     *
     * @param vo
     * @return
     */
    @Override
    public PageInfo<Enter> findPageBreakByCondition(BaseConditionVO vo) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<PakEnter> pakEnters = pakEnterMapper.findPageBreakByCondition(vo);
        if (CollectionUtils.isEmpty(pakEnters)) {
            return null;
        }
        List<Enter> enters = new ArrayList<>();
        for (PakEnter pakEnter : pakEnters) {
            enters.add(new Enter(pakEnter));
        }
        PageInfo bean = new PageInfo<PakEnter>(pakEnters);
        bean.setList(enters);
        return bean;
    }
@Override
    public void increase(PakEnter pakEnter) {
        pakEnterMapper.insert(pakEnter);
    }




}
